from kivy.app import App

from Bases import BaseObject

from bases import widgets


    
class MainWindow(BaseObject):
    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        self.app = KivyApp(MainWindow=self)
        self.app.bind(on_start=self.on_app_start)
        self.app.run()
    def on_app_start(self, *args, **kwargs):
        btn = widgets.Button(label='test1')
        btn.bind(on_press=self.on_btntest_press)
        self.app.root.add_widget(btn)
        
        hbox = widgets.HBox()
        self.testattrib = TestAttribute()
        sld = widgets.VSlider(attribute=self.testattrib)
        hbox.add_widget(sld.widget)
        txt = widgets.Entry(src_object=self.testattrib, src_attr='name', src_signal='name_changed', name='Name')
        hbox.add_widget(txt.topwidget)
        
        self.app.root.add_widget(hbox)
        
        
#        tbl = widgets.Table(cols=4, rows=4, size_hint=(None, None))
#        for i in range(16):
#            btn = widgets.Button(label=str(i+1))
#            tbl.add_widget(btn, index=i)
#        print 'tblsize=', tbl.size
#        scr = widgets.ScrolledWindow(size_hint=(None, None), size=(200, 200))
#        scr.add_widget(tbl)
#        self.app.root.add_widget(scr)
    def on_btntest_press(self, *args, **kwargs):
        print args, kwargs
        btn = widgets.Button(label='test2')
        self.app.root.add_widget(btn)
        
class KivyApp(App):
    def __init__(self, **kwargs):
        self.mainw = kwargs.get('MainWindow')
        super(KivyApp, self).__init__(**kwargs)
    def build(self):
        box = widgets.VBox()
        #btn = widgets.Button(label='test1')
        #btn.bind(on_press=self.mainw.on_btntest_press)
        #box.add_widget(btn)
        return box
    
class TestAttribute(BaseObject):
    def __init__(self, **kwargs):
        self._value = 0
        self._name = 'blah'
        super(TestAttribute, self).__init__(**kwargs)
        self.register_signal('value_changed', 'name_changed')
    @property
    def value(self):
        return self._value
    @value.setter
    def value(self, value):
        self._value = value
        print self, self.value
        self.emit('value_changed', value=value)
    @property
    def name(self):
        return self._name
    @name.setter
    def name(self, value):
        self._name = value
        print self.name
        self.emit('name_changed', obj=self)

if __name__ == '__main__':
    w = MainWindow()
